package org.example.domain.shop.service;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.example.domain.shop.adapter.repository.IShopRepository;
import org.example.domain.shop.model.entity.DiscountCalculationContextEntity;
import org.example.domain.shop.model.entity.DiscountCalculationLinkParamEntity;
import org.example.domain.shop.model.entity.ProductEntity;
import org.example.domain.shop.service.discount.logicLink.DiscountCalculationLogicLink;
import org.example.types.enums.ResponseCode;
import org.example.types.exception.AppException;
import org.example.types.utils.ThreadLocalUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author yinghuashen
 * @version 1.0
 * @description
 * @date 2025/3/6 22:06
 */

@Slf4j
@Service
public class ShopService implements IShopService{

    @Resource
    private IShopRepository shopRepository;

    @Resource
    private DiscountCalculationLogicLink discountCalculationLogicLink;

    @Override
    public ProductEntity getProductById(String productId) {
        if(StringUtils.isBlank(productId)){
            log.error("商品id为空");
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER);
        }
        // TODO 计算折扣价格
        ProductEntity productEntity = shopRepository.getProductById(productId);
        if (productEntity == null) {
            log.error("商品不存在: {}", productId);
            throw new AppException(ResponseCode.NO_PRODUCT);
        }

        // 计算折扣价格
        BigDecimal discountPrice = calculateDiscountPrice(productId, productEntity.getOriginalPrice());
        // 设置折扣价格
        productEntity.setDiscountPrice(discountPrice);

        return productEntity;
    }

    @Override
    public List<ProductEntity> getProductListByKeyword(String keyword, Integer pageNum, Integer pageSize) {
        if(StringUtils.isBlank(keyword)){
            log.error("搜索关键字为空");
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER);
        }
        List<ProductEntity> productEntities = shopRepository.getProductListByKeyword(keyword, pageNum, pageSize);
        for (ProductEntity productEntity : productEntities) {
            // 计算折扣价格
            BigDecimal discountPrice = calculateDiscountPrice(productEntity.getProductId(), productEntity.getOriginalPrice());
            // 设置折扣价格
            productEntity.setDiscountPrice(discountPrice);
        }
        return productEntities;
    }

    @Override
    public BigDecimal calculateDiscountPrice(String productId, BigDecimal originalPrice) {
        DiscountCalculationLinkParamEntity paramEntity = new DiscountCalculationLinkParamEntity();
        paramEntity.setUserId(ThreadLocalUtils.getCurrentUserId());
        paramEntity.setProductId(productId);
        paramEntity.setOriginalPrice(originalPrice);

        DiscountCalculationContextEntity contextEntity = new DiscountCalculationContextEntity();
        contextEntity.setProductId(paramEntity.getProductId());

        BigDecimal discount = discountCalculationLogicLink.getLogicLink().execute(paramEntity, contextEntity);
        return originalPrice.subtract(discount);
    }

}
